/**
 *    Copyright 2009-2019 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.session;

import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.executor.BatchResult;

import java.io.Closeable;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

/**
 * 在Mybatis框架中针对JDBC操作进行封装的对象,用于在Mybatis中替代{@link Connection}。
 *
 * SqlSession对象是Mybatis提供给用户进行数据库操作的顶层API接口，
 * 它定义了数据库操作的方法以及事务控制的方法，在Mybatis里面，
 * 在一定程度上我们可以认为SqlSession对象对标JDBC的Connection对象。
 *
 * <p>
 * The primary Java interface for working with MyBatis.
 * Through this interface you can execute commands, get mappers and manage transactions.
 *
 * @author Clinton Begin
 */
public interface SqlSession extends Closeable {

    /**
     * 查找一个实例
     * Retrieve a single row mapped from the statement key
     *
     * @param <T>       the returned object type 返回类型
     * @param statement Statement语句唯一标志
     * @return Mapped object 查询出来的结果
     */
    <T> T selectOne(String statement);

    /**
     * 查找一个实例
     * Retrieve a single row mapped from the statement key and parameter.
     *
     * @param <T>       the returned object type 返回类型
     * @param statement Unique identifier matching the statement to use.   Statement语句唯一标志
     * @param parameter A parameter object to pass to the statement.
     * @return Mapped object 查询出来的结果
     */
    <T> T selectOne(String statement, Object parameter);

    /**
     * 查询出一组对象集合
     * Retrieve a list of mapped objects from the statement key and parameter.
     *
     * @param <E>       the returned list element type 返回对象类型
     * @param statement Unique identifier matching the statement to use. Statement语句唯一标志
     * @return List of mapped object 查询出来的结果
     */
    <E> List<E> selectList(String statement);

    /**
     * 查询出一组对象集合
     * Retrieve a list of mapped objects from the statement key and parameter.
     *
     * @param <E>       the returned list element type 返回对象类型
     * @param statement Unique identifier matching the statement to use. Statement语句唯一标志
     * @param parameter A parameter object to pass to the statement. 要传递给statement语句的参数
     * @return List of mapped object 对象列表
     */
    <E> List<E> selectList(String statement, Object parameter);

    /**
     * 查询出一组对象集合
     * Retrieve a list of mapped objects from the statement key and parameter,
     * within the specified row bounds.
     *
     * @param <E>       the returned list element type 返回对象类型
     * @param statement Unique identifier matching the statement to use. Statement语句唯一标志
     * @param parameter A parameter object to pass to the statement. 要传递给statement语句的参数
     * @param rowBounds Bounds to limit object retrieval 限制查询结果的对象
     * @return List of mapped object 对象列表
     */
    <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds);

    /**
     * 查询Map集合
     * The selectMap is a special case in that it is designed to convert a list
     * of results into a Map based on one of the properties in the resulting
     * objects.
     * Eg. Return a of Map[Integer,Author] for selectMap("selectAuthors","id")
     *
     * @param <K>       the returned Map keys type      Map-key类型
     * @param <V>       the returned Map values type    Map-value类型
     * @param statement Unique identifier matching the statement to use. Statement语句唯一标志
     * @param mapKey    The property to use as key for each value in the list.
     * @return Map containing key pair data.  Map集合
     */
    <K, V> Map<K, V> selectMap(String statement, String mapKey);

    /**
     * 查询Map集合
     * The selectMap is a special case in that it is designed to convert a list
     * of results into a Map based on one of the properties in the resulting
     * objects.
     *
     * @param <K>       the returned Map keys type      Map-key类型
     * @param <V>       the returned Map values type    Map-value类型
     * @param statement Unique identifier matching the statement to use. Statement语句唯一标志
     * @param parameter A parameter object to pass to the statement. 参数
     * @param mapKey    The property to use as key for each value in the list.
     * @return Map containing key pair data.  Map集合
     */
    <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey);

    /**
     * 查询Map集合
     * The selectMap is a special case in that it is designed to convert a list
     * of results into a Map based on one of the properties in the resulting
     * objects.
     *
     * @param <K>       the returned Map keys type      Map-key类型
     * @param <V>       the returned Map values type    Map-value类型
     * @param statement Unique identifier matching the statement to use. Statement语句唯一标志
     * @param parameter A parameter object to pass to the statement. 参数
     * @param mapKey    The property to use as key for each value in the list.
     * @param rowBounds Bounds to limit object retrieval    分页数据
     * @return Map containing key pair data.  Map集合
     */
    <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds);

    /**
     * 查询游标
     * A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.
     *
     * @param <T>       the returned cursor element type. 游标内部对象类型
     * @param statement Unique identifier matching the statement to use. Statement语句唯一标志
     * @return Cursor of mapped objects  映射对象的游标
     */
    <T> Cursor<T> selectCursor(String statement);

    /**
     * 查询游标
     * A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.
     *
     * @param <T>       the returned cursor element type. 游标内部对象类型
     * @param statement Unique identifier matching the statement to use. Statement语句唯一标志
     * @param parameter A parameter object to pass to the statement. 用于声明语句的参数
     * @return Cursor of mapped objects  映射对象的游标
     */
    <T> Cursor<T> selectCursor(String statement, Object parameter);

    /**
     * 查询游标
     * A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.
     *
     * @param <T>       the returned cursor element type. 游标内部对象类型
     * @param statement Unique identifier matching the statement to use. Statement语句唯一标志
     * @param parameter A parameter object to pass to the statement. 用于声明语句的参数
     * @param rowBounds Bounds to limit object retrieval 分页参数
     * @return Cursor of mapped objects 映射对象的游标
     */
    <T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds);

    /**
     * 使用ResultHandler查询一个对象
     * Retrieve a single row mapped from the statement key and parameter
     * using a {@code ResultHandler}.
     *
     * @param statement Unique identifier matching the statement to use. Statement语句唯一标志
     * @param parameter A parameter object to pass to the statement. 参数对象
     * @param handler   ResultHandler that will handle each retrieved row. ResultHandler
     */
    void select(String statement, Object parameter, ResultHandler handler);

    /**
     * 使用ResultHandler查询一个对象
     * Retrieve a single row mapped from the statement
     * using a {@code ResultHandler}.
     *
     * @param statement Unique identifier matching the statement to use. Statement语句唯一标志
     * @param handler   ResultHandler that will handle each retrieved row. ResultHandler
     */
    void select(String statement, ResultHandler handler);

    /**
     * 使用ResultHandler查询一个对象
     * Retrieve a single row mapped from the statement key and parameter
     * using a {@code ResultHandler} and {@code RowBounds}
     *
     * @param statement Unique identifier matching the statement to use. Statement语句唯一标志
     * @param rowBounds RowBound instance to limit the query results . 分页对象
     * @param handler   ResultHandler that will handle each retrieved row . ResultHandler
     */
    void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler);

    /**
     * 执行一条插入语句
     * Execute an insert statement.
     *
     * @param statement Unique identifier matching the statement to execute. Statement语句唯一标志
     * @return int The number of rows affected by the insert. 插入的行数
     */
    int insert(String statement);

    /**
     * 根据指定的参数插入一条语句
     * Execute an insert statement with the given parameter object. Any generated
     * autoincrement values or selectKey entries will modify the given parameter
     * object properties. Only the number of rows affected will be returned.
     *
     * @param statement Unique identifier matching the statement to execute.   Statement语句唯一标志
     * @param parameter A parameter object to pass to the statement. 参数对象
     * @return int The number of rows affected by the insert.  插入的行数
     */
    int insert(String statement, Object parameter);

    /**
     * 执行一条Update语句.
     * <p>
     * Execute an update statement. The number of rows affected will be returned.
     *
     * @param statement Unique identifier matching the statement to execute. Statement语句唯一标志
     * @return int The number of rows affected by the update. 更新的行数
     */
    int update(String statement);

    /**
     * 根据指定的参数执行一条更新语句
     * Execute an update statement. The number of rows affected will be returned.
     *
     * @param statement Unique identifier matching the statement to execute. Statement语句唯一标志
     * @param parameter A parameter object to pass to the statement. 参数
     * @return int The number of rows affected by the update. 更新的行数
     */
    int update(String statement, Object parameter);

    /**
     * 执行一条删除语句
     * Execute a delete statement. The number of rows affected will be returned.
     *
     * @param statement Unique identifier matching the statement to execute. Statement语句唯一标志
     * @return int The number of rows affected by the delete. 删除的行数
     */
    int delete(String statement);

    /**
     * 根据指定的参数执行一条删除语句
     * Execute a delete statement. The number of rows affected will be returned.
     *
     * @param statement Unique identifier matching the statement to execute. Statement语句唯一标志
     * @param parameter A parameter object to pass to the statement. 参数
     * @return int The number of rows affected by the delete. 删除的行数
     */
    int delete(String statement, Object parameter);

    /**
     * 刷新批处理语句并提交
     * Flushes batch statements and commits database connection.
     * Note that database connection will not be committed if no updates/deletes/inserts were called.
     * To force the commit call {@link SqlSession#commit(boolean)}
     */
    void commit();

    /**
     * 是否强制刷新批处理语句并提交
     * Flushes batch statements and commits database connection.
     *
     * @param force forces connection commit 强制提交
     */
    void commit(boolean force);

    /**
     * 丢弃待处理的批处理语句并回滚
     * Discards pending batch statements and rolls database connection back.
     * Note that database connection will not be rolled back if no updates/deletes/inserts were called.
     * To force the rollback call {@link SqlSession#rollback(boolean)}
     */
    void rollback();

    /**
     * 是否强制丢弃待处理的批处理语句并回滚
     * Discards pending batch statements and rolls database connection back.
     * Note that database connection will not be rolled back if no updates/deletes/inserts were called.
     *
     * @param force forces connection rollback 强制回滚
     */
    void rollback(boolean force);

    /**
     * 刷新批处理语句
     * Flushes batch statements.
     *
     * @return BatchResult list of updated records
     * @since 3.0.6
     */
    List<BatchResult> flushStatements();

    /**
     * 关闭当前会话
     * Closes the session
     */
    @Override
    void close();

    /**
     * 清理本地缓存
     * Clears local session cache
     */
    void clearCache();

    /**
     * 获取当前配置
     * Retrieves current configuration
     *
     * @return Configuration
     */
    Configuration getConfiguration();

    /**
     * 获取一个映射器
     * Retrieves a mapper.
     *
     * @param <T>  the mapper type 映射器的类型
     * @param type Mapper interface class mapper接口类型
     * @return a mapper bound to this SqlSession  绑定到此SqlSession的映射器
     */
    <T> T getMapper(Class<T> type);

    /**
     * 获取一个数据库链接
     * Retrieves inner database connection
     *
     * @return Connection 数据库链接
     */
    Connection getConnection();
}
